package com.wkbb.sports.service.impl;

import com.wkbb.sports.dto.*;
import com.wkbb.common.enums.RoleTypeEnum;
import com.wkbb.sports.mapper.HlifeAchievementMapper;
import com.wkbb.sports.mapper.HlifeAchievementStatMapper;
import com.wkbb.sports.mapper.HlifeTermMapper;
import com.wkbb.sports.mapper.HlifeUserMapper;
import com.wkbb.sports.model.HlifeAchievementStat;
import com.wkbb.sports.model.HlifeTerm;
import com.wkbb.sports.model.HlifeUser;
import com.wkbb.sports.service.ReportService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {

    @Autowired
    private HlifeUserMapper hlifeUserMapper;

    @Autowired
    private HlifeTermMapper hlifeTermMapper;

    @Autowired
    private HlifeAchievementMapper hlifeAchievementMapper;

    @Autowired
    private HlifeAchievementStatMapper achievementStatMapper;

    @Override
    public Map<String,Object> getUserHlifeReport(Long orgId, Long termId, Long userId)
    {
        Map<String,Object> map = new HashMap<>();

        //查询用户信息
        Map<String, Object> userParams = new HashMap<String, Object>();
        userParams.put("orgId", orgId);
        userParams.put("userId", userId);
        userParams.put("userType", (byte) RoleTypeEnum.STUDENT.getValue());//用户类型(1教师 2家长 3学生)
        HlifeUser hlifeUser = hlifeUserMapper.getByMapParams(userParams);
        map.put("hlifeUser",hlifeUser);
        if(null==userId)
        {
            if(null==hlifeUser)
            {
                return new HashMap<String, Object>();
            }
            userId = hlifeUser.getUserId();
        }

        //查询考期
        Long currentTermId = null;
        Long lastTermId = null;
        if(null!=termId)
        {
            currentTermId = termId;
        }

        //查询上一期信息
        Map<String, Object> termParams = new HashMap<String, Object>();
        termParams.put("orgId", orgId);
        termParams.put("userId", userId);
        List<HlifeTerm> hlifeTermList = hlifeAchievementMapper.getTermLatelyList(termParams);
        if (!hlifeTermList.isEmpty()) {
            if(null==currentTermId) {
                currentTermId = hlifeTermList.get(0).getId();
            }

            if (hlifeTermList.size() == 2) {
                lastTermId = hlifeTermList.get(1).getId();
            }
        }

        //查询综合得分
        Map<String, Object> userScoreStatParams = new HashMap<String, Object>();
        userScoreStatParams.put("orgId", orgId);
        userScoreStatParams.put("userId", userId);
        userScoreStatParams.put("termId", currentTermId);
        HlifeAchievementStat hlifeAchievementStat = achievementStatMapper.getByMapParams(userScoreStatParams);
        map.put("achievementStat",hlifeAchievementStat);

        //体检统计
        Map<String, Object> examineMap = this.getUserExamineAchievement(orgId, userId, hlifeUser, currentTermId, lastTermId);
        map.put("examineStat",examineMap);

        //体质成绩统计
        Map<String,Object> physicalMap = this.getUserPhysicalAchievement(orgId, userId, hlifeUser, currentTermId, lastTermId);
        map.put("physicalStat",physicalMap);

        return map;
    }

    private Map<String, Object> getUserExamineAchievement(Long orgId, Long userId, HlifeUser hlifeUser, Long currentTermId, Long lastTermId) {
        Map<String,Object> examineMap = new HashMap<>();

        //本期体检成绩
        Map<String, Object> nearestExamineParams = new HashMap<String, Object>();
        nearestExamineParams.put("orgId", orgId);
        nearestExamineParams.put("userId", userId);
        nearestExamineParams.put("termId", currentTermId);
        List<AchievementProjectDto> nearestAchievementList = hlifeAchievementMapper.getUserExamineAchievementList(nearestExamineParams);
        examineMap.put("nearestAchievementList",nearestAchievementList);

        //上次体检成绩
        List<AchievementProjectDto> lastAchievementList = new ArrayList<>();
        if(null!=lastTermId)
        {
            Map<String, Object> lastExamineParams = new HashMap<String, Object>();
            lastExamineParams.put("orgId", orgId);
            lastExamineParams.put("userId", userId);
            lastExamineParams.put("termId", lastTermId);
            lastAchievementList = hlifeAchievementMapper.getUserExamineAchievementList(lastExamineParams);
        }
        examineMap.put("lastAchievementList",lastAchievementList);

        //年级体检成绩
        List<AchievementProjectDto> gradeAchievementList = new ArrayList<>();
        if(null!=currentTermId)
        {
            Map<String, Object> gradeExamineParams = new HashMap<String, Object>();
            gradeExamineParams.put("orgId", orgId);
            gradeExamineParams.put("gradeId", hlifeUser.getGradeId());
            gradeExamineParams.put("termId", currentTermId);
            gradeExamineParams.put("sex", hlifeUser.getSex());
            gradeAchievementList = hlifeAchievementMapper.getUserExamineAvgAchievementList(gradeExamineParams);
        }
        examineMap.put("gradeAchievementList",gradeAchievementList);
        return examineMap;
    }

    private Map<String, Object> getUserPhysicalAchievement(Long orgId, Long userId, HlifeUser hlifeUser, Long currentTermId, Long lastTermId) {
        Map<String,Object> physicalMap = new HashMap<>();

        //本期体检成绩
        Map<String, Object> nearestPhysicalParams = new HashMap<String, Object>();
        nearestPhysicalParams.put("orgId", orgId);
        nearestPhysicalParams.put("userId", userId);
        nearestPhysicalParams.put("termId", currentTermId);
        List<AchievementProjectDto> nearestAchievementList = hlifeAchievementMapper.getUserPhysicalAchievementList(nearestPhysicalParams);
        physicalMap.put("nearestAchievementList",nearestAchievementList);

        //上次体检成绩
        List<AchievementProjectDto> lastAchievementList = new ArrayList<>();
        if(null!=lastTermId)
        {
            Map<String, Object> lastPhysicalParams = new HashMap<String, Object>();
            lastPhysicalParams.put("orgId", orgId);
            lastPhysicalParams.put("userId", userId);
            lastPhysicalParams.put("termId", lastTermId);
            lastAchievementList = hlifeAchievementMapper.getUserPhysicalAchievementList(lastPhysicalParams);
        }
        physicalMap.put("lastAchievementList",lastAchievementList);

        //年级体检成绩
        List<AchievementProjectDto> gradeAchievementList = new ArrayList<>();
        if(null!=currentTermId)
        {
            Map<String, Object> gradePhysicalParams = new HashMap<String, Object>();
            gradePhysicalParams.put("orgId", orgId);
            gradePhysicalParams.put("gradeId", hlifeUser.getGradeId());
            gradePhysicalParams.put("termId", currentTermId);
            gradePhysicalParams.put("sex", hlifeUser.getSex());
            gradeAchievementList = hlifeAchievementMapper.getUserPhysicalAvgAchievementList(gradePhysicalParams);
        }
        physicalMap.put("gradeAchievementList",gradeAchievementList);

        return physicalMap;
    }
}
